import React from 'react';
import { Row, Col, message, Spin } from 'antd';
// import { TERMINATE_TYPE } from '@/pages/Recruitment/dictionary';
import moment from 'moment';
import { HireCard } from '../containers';
import { NoticeLink } from '../containers/StatusDetailLink';
import { getRecruitmentInfo } from '../service';
import { sendTypeMap, recruitStatusInfo } from '../utils';
import SalaryCard from './SalaryCard';
import ApprovalCard from './approvalCard';
import styles from './index.less';

/**
 *  简历详情，录用tab
 * @param {Function} onHireData 设置父组件中招聘状态的函数，1=待处理 2=需协商 3=审批中 4=审批拒绝 5=审批撤回 6=待接收  7=已接受 8=已取消 9=已入职 10已淘汰 99=其他
 */
const HireDetail = ({ employId, candidateId, hireDetailRef, onHireData }) => {
    const [loading, setLoading] = React.useState();
    const [data, setData] = React.useState({}); // 本页面内容主要靠这个

    const { employApply = {}, recruitTalentEmploy, employStatusHover } = data;

    const recruitStatus = recruitTalentEmploy?.employStatus;

    const getText = (arr, value) => {
        const target = arr.find(item => item.value === value);
        return target?.text || '';
    };

    const getData = async () => {
        if (!employId) {
            return;
        }
        setLoading(true);
        const res = await getRecruitmentInfo({ employId });
        if (res.code === 10000) {
            setData(res.data?.auditRecord || {});
            if (onHireData) {
                onHireData({ recruitStatus: res.data?.auditRecord?.recruitTalentEmploy?.employStatus });
            }
        } else {
            message.error(res.msg);
        }
        setLoading(false);
    };
    React.useEffect(() => {
        getData();
    }, [employId]);

    React.useImperativeHandle(hireDetailRef, () => ({
        runHireDetail: () => getData()
    }));

    const successNoticeLink = {
        text: '已接受',
        color: '#44c69d',
        background: '#ecfff7',
        link: params => <NoticeLink params={params} />
    };

    const employApplyFailInfo = {
        text: '已淘汰',
        color: 'rgba(0,0,0,0.45)',
        background: 'rgba(0, 0, 0, 0.05)',
        link: params => <NoticeLink params={params} />
    };

    // const onboardFailInfo = {
    //     text: '已淘汰',
    //     color: 'rgba(0,0,0,0.45)',
    //     background: 'rgba(0, 0, 0, 0.05)'
    // };

    return (
        <Spin spinning={loading} style={{ marginTop: loading ? '50px' : '' }}>
            {recruitStatus > 8 && recruitStatus !== 10 && (
                <HireCard
                    title="入职记录"
                    recruitStatus={recruitStatus}
                    showFoot={false}
                    // showFoot={recruitStatus === 10}
                    // failInfo={recruitStatus === 10 ? onboardFailInfo : ''}
                >
                    <div className={styles.infoContent} style={{ marginBottom: '16px' }}>
                        <Row className={styles.infoRow}>
                            <Col span={12}>
                                <span className={styles.label}>入职状态：</span>
                                <span className={styles.value}>
                                    {getText(recruitStatusInfo, recruitTalentEmploy.employStatus)}
                                </span>
                            </Col>
                            <Col span={12}>
                                <span className={styles.label}>入职时间：</span>
                                <span className={styles.value}>
                                    {recruitTalentEmploy.entryTime
                                        ? moment(recruitTalentEmploy.entryTime).format('YYYY-MM-DD HH:mm')
                                        : ''}
                                </span>
                            </Col>
                        </Row>
                    </div>
                </HireCard>
            )}
            {recruitStatus > 5 && employApply.sendTime && (
                <HireCard
                    title="录用通知发送记录"
                    linkParams={{
                        employId,
                        action: 'detail',
                        candidateId
                    }}
                    recruitStatus={recruitStatus}
                    successFootInfo={recruitStatus === 7 || recruitStatus > 8 ? successNoticeLink : ''}
                    failInfo={recruitStatus === 10 ? employApplyFailInfo : ''}
                >
                    <div className={styles.infoContent}>
                        <Row className={styles.infoRow}>
                            <Col span={12}>
                                <span className={styles.label}>发送方式：</span>
                                <span className={styles.value}>{`${sendTypeMap[employApply.sendType] || ''}`}</span>
                            </Col>
                            <Col span={12}>
                                <span className={styles.label}>发送时间：</span>
                                <span className={styles.value}>{`${recruitTalentEmploy.againEmployedTime ||
                                    employApply.sendTime ||
                                    ''}`}</span>
                            </Col>
                        </Row>
                        {!employApply.recruitStatus === 7 && (
                            <Row className={styles.infoRow}>
                                <>
                                    <Col span={12}>
                                        <span className={styles.label}>接受情况：</span>
                                        <span className={styles.value}>
                                            {getText(recruitStatusInfo, employApply.recruitStatus)}
                                        </span>
                                    </Col>
                                    <Col span={12}>
                                        <span className={styles.label}>理由：</span>
                                        <span className={styles.value}>{employApply.refuseReason}</span>
                                    </Col>
                                </>
                            </Row>
                        )}
                    </div>
                </HireCard>
            )}

            {recruitStatus > 2 && !!employApply.hiDataId && (
                <ApprovalCard
                    recruitStatus={recruitStatus}
                    employApply={employApply}
                    candidateId={candidateId}
                    employId={employId}
                    employStatusHover={employStatusHover}
                    recruitTalentEmploy={recruitTalentEmploy}
                />
            )}

            {recruitStatus && (
                <SalaryCard
                    employApply={employApply}
                    recruitStatus={recruitStatus}
                    employId={employId}
                    candidateId={candidateId}
                />
            )}
        </Spin>
    );
};

export default HireDetail;
